package com.winit.feature.repository;

import com.datastax.driver.core.querybuilder.QueryBuilder;
import com.datastax.driver.core.querybuilder.Select;
import com.datastax.driver.core.querybuilder.Update;
import com.winit.feature.model.Feature;
import com.winit.feature.model.FeatureReq;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

/**
 * Created by wangjian on 2017/9/28.
 */
@Component
public class FeatureOperations {

    private static Logger logger = LoggerFactory.getLogger(FeatureOperations.class);

    private final String TABLE = "matrix_feature_new";

//    private AtomicInteger num = new AtomicInteger(0);
//
//    private AtomicLong time = new AtomicLong(0);

    @Autowired
    private CassandraOperations cassandraOperations;

    @Autowired
    FeatureRepository repository;

    public void updateFeature(Feature feature) {
        cassandraOperations.update(feature);
    }

    public Feature getFeatureById(String id) {
        return cassandraOperations.selectOneById(Feature.class, id);
    }

//    public List<Feature> getFeatureByParentId(String parentId) {
//        return repository.findAllByParentId(parentId);
//    }


    public List<Feature> findAllByFeatureSysAndId(String parentId, String featureSys, String featureId, String changeTypeExclude) {
//        logger.info(num.incrementAndGet() + "<<<<<<<<<<<<<<<<<<<<<<<<<");
//        Long start = System.currentTimeMillis();
        Select select = QueryBuilder.select().from(TABLE);
        if (null != parentId) {
            select.where(QueryBuilder.eq("parent_id", parentId));
        }
        if (null != featureSys) {
            select.where(QueryBuilder.eq("sys", featureSys));
        }
        if (null != featureId) {
            select.where(QueryBuilder.eq("feature_id", featureId));
        }
//        select.where(QueryBuilder.eq("feature_req_id", "-1"));
        select.allowFiltering();
        List<Feature> features = cassandraOperations.select(select, Feature.class);
        if (CollectionUtils.isEmpty(features)) {
            return null;
        }
//        Iterator<Feature> iterator = features.iterator();
//        while (iterator.hasNext()){
//            if (null != changeTypeExclude && changeTypeExclude.equalsIgnoreCase(iterator.next().getFeatureChangeType())){
//                iterator.remove();
//            }
//        }
//        Long end = System.currentTimeMillis();
//        System.out.println();
//        logger.info(time.addAndGet(end - start) + ">>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        return CollectionUtils.isEmpty(features) ? null : features;
    }


    public int updatePropertiesNotNullById(Feature feature) {
        Update update = QueryBuilder.update(TABLE);
//        if (feature.getSys() != null) {
//            update.with(QueryBuilder.set("sys", feature.getSys()));
//        }
        if (feature.getPath() != null) {
            update.with(QueryBuilder.set("path", feature.getPath()));
        }
        if (feature.getStatus() != null) {
            update.with(QueryBuilder.set("status", feature.getStatus()));
        }
        if (null != feature.getAttrConfigId()) {
            update.with(QueryBuilder.set("attr_config_id", feature.getAttrConfigId()));
        }
        if (null != feature.getReqId()) {
            update.with(QueryBuilder.set("req_id", feature.getReqId()));
        }
        /**
         * 2017-11-29，将feature_name由config移向feature中
         */
        if (null != feature.getFeatureName()) {
            update.with(QueryBuilder.set("feature_name", feature.getFeatureName()));
        }

        if (null != feature.getType()) {
            update.with(QueryBuilder.set("type", feature.getType()));
        }

        update.where(QueryBuilder.eq("id", feature.getId()));
        cassandraOperations.execute(update);
        return 1;
    }

    public List<Feature> findByProperNotNull(Feature feature) {
        Select select = QueryBuilder.select().from(TABLE);
        if (feature.getId() != null) {
            select.where(QueryBuilder.eq("id", feature.getId()));
        }
        if (feature.getSys() != null) {
            select.where(QueryBuilder.eq("sys", feature.getSys()));
        }
        if (feature.getPath() != null) {
            select.where(QueryBuilder.eq("path", feature.getPath()));
        }
        if (feature.getStatus() != null) {
            select.where(QueryBuilder.eq("status", feature.getStatus()));
        }
        if (null != feature.getAttrConfigId()) {
            select.where(QueryBuilder.eq("attr_config_id", feature.getAttrConfigId()));
        }
        if (null != feature.getReqId()) {
            select.where(QueryBuilder.eq("req_id", feature.getReqId()));
        }
        /**
         * 2017-11-29，将feature_name由config移向feature中
         */
        if (null != feature.getFeatureName()) {
            select.where(QueryBuilder.eq("feature_name", feature.getFeatureName()));
        }
        select.allowFiltering();

        return cassandraOperations.select(select, Feature.class);
    }


    public List<FeatureReq> findFeatureReqByFeatureId(String featureId) {
        Select select = QueryBuilder.select().from("matrix_feature_req");
        select.where(QueryBuilder.eq("feature_id", featureId));
        select.allowFiltering();
        return cassandraOperations.select(select, FeatureReq.class);
    }

}
